package com.gdface.admin.gdface_attend;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import net.gdface.face.DaoMaster;
import net.gdface.face.DaoSession;

/**
 * Created by admin on 2017/12/27.
 */

public class MyApplication extends Application {

    private static Context mThis;
    private static String DB_name = "face.db";
    private static DaoSession daoSession;

//    private static String IP = "192.168.1.76";
//    public static IFaceLogClient faceLogClient;
//    /** 设备心跳监控频道 */
//    private static String monitorChannelName;
//
//    /**设备令牌*/
//    private Token deviceToken;
//
//    /** redis 连接参数 */
//    private static final Map<PropName, Object> redisParam = ImmutableMap.<PropName, Object>of(
//            /** redis 主机名 */
//            PropName.host, IP,
//            /** redis 端口号 */
//            PropName.port, Protocol.DEFAULT_PORT
//            /** redis 连接密码 */
////			,PropName.password, null
//    );


    @Override
    public void onCreate() {
        super.onCreate();

        mThis = this;
        setupDatabase();


//        faceLogClient = ClientFactory.builder().setHostAndPort(IP, CommonConstant.DEFAULT_PORT).build();
//        //获取服务端版本
//        faceLogClient.version();
//
//        JedisPoolLazy.createDefaultInstance( redisParam);
//        // 默认实例创建后，后续可以调用 JedisPoolLazy.getDefaultInstance() 获取并使用这个默认实例
//
//        RegistDevice();

    }

    @Override
    public void onTerminate() {
        //程序终止时调用

        // 应用结束时通知facelog servcie设备下线，释放设备令牌
//        try {
//            faceLogClient.offline(deviceToken);
//        } catch (ServiceSecurityException e) {
//            System.out.println(e.getServiceStackTraceMessage());
//        }
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        // 低内存的时候执行
        super.onLowMemory();
    }
    @Override
    public void onTrimMemory(int level) {
        // 程序在内存清理的时候执行
        super.onTrimMemory(level);
    }

//    private void RegistDevice(){
//        try {
//            // 获取当前设备的MAC地址(假设只有一块网卡)
//            byte[] addr = NetworkUtil.getPhysicalNICs().iterator().next().getHardwareAddress();
//            String mac = NetworkUtil.formatMac(addr,null);
//            Log.i("hwwlog","amc = "+mac+"\naddr = "+addr);
//
//            DeviceBean deviceBean = DeviceBean.builder()
//                    .name("测试设备1")
//                    .mac(mac)
//                    .serialNo(Build.SERIAL)
//                    .version(Build.VERSION.RELEASE)
//                    .remark("")
//                    .build();
//            //设备注册
//            deviceBean = faceLogClient.registerDevice(deviceBean);
//            Log.i("hwwlog","deviceBean = "+deviceBean.toString());
//            //申请设备令牌
//            deviceToken = faceLogClient.online(deviceBean);
//        } catch (SocketException e) {
//            e.printStackTrace();
//        } catch (ServiceSecurityException e) {
//            System.out.println(e.getServiceStackTraceMessage());
//        }
//
//    }



    /**
     * 配置数据库
     */
    private static void setupDatabase() {
        //创建数据库
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(mThis, DB_name, null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoSession(){
        if(null == daoSession){
            setupDatabase();
        }
        return daoSession;
    }

}
